/*
 * File Name：PayMethodContainer.java
 * Copyright：Copyright 2008-2014 CiWong.Inc. All Rights Reserved.
 * Description： PayMethodContainer.java
 * Modify By：jejun
 * Modify Date：2014-6-12
 * Modify Type：Add
 */
package com.ciwong.mobilepay.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.content.res.ResourcesCompat;
import android.text.InputType;
import android.text.TextUtils.TruncateAt;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.view.inputmethod.EditorInfo;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciwong.mobilelib.R;
import com.ciwong.mobilepay.util.PayUtil;

/**
 * 支付方式
 * 
 * @author jejun
 * @version ciwong v.1.0 2014-6-12
 * @since ciwong v.1.0
 */
public class EPayMethodContainer extends LinearLayout
{
    public static final int payMethodVirtual = 0xff0f1;

    public static final int payMethodLack = 0xff0f2;

    public static final int loading = 0xff0f3;

    public static final int balanceContainer = 0xff0f4;

    public static final int payMethodLV = 0xff0f5;

    public static final int payPasswordContainer = 0xff0f6;

    public static final int payMethodPassword = 0xff0f7;

    public static final int payMethodPasswordET = 0xff0f8;

    public static final int payMethodVirtualIv = 0xff0f9;

    public static final int payMethodVirtualContainer = 0xff0f10;

    public static final int payMethodVirtualTv = 0xff0f11;
    
    private int itemCount = 3;

    public EPayMethodContainer(Context context, int itemCount)
    {
        super(context);
        this.itemCount = itemCount;
        init();
    }

    private void init()
    {
        setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        setOrientation(LinearLayout.VERTICAL);

        View line = new View(getContext());
        line.setBackgroundDrawable(
//                PayUtil.getDrawableFromJar(
//                "/images_mobilepay/line.png", EPayMethodContainer.class)
                ResourcesCompat.getDrawable(getResources(), R.mipmap.line, null)

        );


        LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, 1);
        addView(line);
        line.setLayoutParams(lp);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        TextView txt = new TextView(getContext());
        lp = new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);
        int padding = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 15, dm);
        txt.setPadding(padding, padding, padding, padding);
        txt.setTextColor(0xff747474);
        txt.setText("支付方式");
        txt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        addView(txt);
        txt.setLayoutParams(lp);

        RelativeLayout rl = new RelativeLayout(getContext());
        rl.setId(EPayMethodContainer.payMethodVirtualContainer);
        lp = new LayoutParams(LayoutParams.FILL_PARENT,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                        70, dm));
        lp.setMargins(0, 0, 0, padding);
        rl.setLayoutParams(lp);
        rl.setBackgroundColor(Color.WHITE);
        addView(rl);
        rl.setVisibility(View.GONE);
        padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                10, dm);
        // rl.setPadding(padding, padding, padding, padding);

        line = new View(getContext());
        line.setBackgroundDrawable(
//                PayUtil.getDrawableFromJar(
//                "/images_mobilepay/line.png", EPayMethodContainer.class)
                ResourcesCompat.getDrawable(getResources(), R.mipmap.line, null)

        );


        lp = new LayoutParams(LayoutParams.FILL_PARENT, 1);
        line.setLayoutParams(lp);

        int width = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 30, dm);
        Rect rect = new Rect(0, 0, width, width);
//        Drawable chekced = PayUtil.getDrawableFromJar(
//                "/images_mobilepay/check_circle_checked.png",
//                EPayMethodContainer.class);
//        Drawable chekced = getContext().getDrawable(R.mipmap.check_circle_checked);
        Drawable chekced= ResourcesCompat.getDrawable(getResources(), R.mipmap.check_circle_checked, null);
        chekced.setBounds(rect);
//        Drawable press = PayUtil.getDrawableFromJar(
//                "/images_mobilepay/check_circle_checked.png",
//                EPayMethodContainer.class);
        Drawable press =ResourcesCompat.getDrawable(getResources(), R.mipmap.check_circle_checked, null);
        press.setBounds(rect);
//        Drawable normal = PayUtil.getDrawableFromJar(
//                "/images_mobilepay/check_circle_normal.png",
//                EPayMethodContainer.class);
        Drawable normal =ResourcesCompat.getDrawable(getResources(), R.mipmap.check_circle_checked, null);


        normal.setBounds(rect);

        Drawable selector = PayUtil.getDrawableSelector(new int[][]{
                {android.R.attr.state_checked}, {android.R.attr.state_pressed},
                {android.R.attr.state_selected}, {}}, new Drawable[]{chekced,
                press, press, normal});

        RelativeLayout.LayoutParams rllp = new RelativeLayout.LayoutParams(
                width, width);
        ImageView payMethodVietualIv = new ImageView(getContext());
        rllp.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
        rllp.setMargins(padding, 0, padding, 0);

        payMethodVietualIv.setId(EPayMethodContainer.payMethodVirtualIv);
        payMethodVietualIv.setLayoutParams(rllp);
        payMethodVietualIv.setBackgroundDrawable(selector);
        // payMethodVietualIv.setImageDrawable(selector);

        payMethodVietualIv.setScaleType(ScaleType.FIT_CENTER);

        rllp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);
        final TextView payMethodVirtual = new TextView(getContext());
        payMethodVirtual.setId(EPayMethodContainer.payMethodVirtualTv);
        rllp.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
        rllp.addRule(RelativeLayout.RIGHT_OF,
                EPayMethodContainer.payMethodVirtualIv);
        payMethodVirtual.setText("可用习币:");
        payMethodVirtual.setTextColor(Color.BLACK);
        payMethodVirtual.setEnabled(false);

        // payMethodVirtual.setButtonDrawable(new
        // ColorDrawable(Color.TRANSPARENT));
        // payMethodVirtual.setCompoundDrawablesWithIntrinsicBounds(0, 0, 50,
        // 50);
        payMethodVirtual.setLayoutParams(rllp);
        rl.addView(line);
        rl.addView(payMethodVietualIv);
        rl.addView(payMethodVirtual);
        final CheckBox cbVirtual = new CheckBox(getContext());
        cbVirtual.setId(EPayMethodContainer.payMethodVirtual);
        cbVirtual.setVisibility(View.GONE);
        rl.addView(cbVirtual);
        // payMethodVietualIv.setOnClickListener(new OnClickListener()
        // {
        //
        // @Override
        // public void onClick(View v)
        // {
        // cbVirtual.setChecked(!cbVirtual.isChecked());
        // }
        // });
        rl.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                cbVirtual.setChecked(!cbVirtual.isChecked());
            }
        });

        TextView payMethodLack = new TextView(getContext());
        payMethodLack.setId(EPayMethodContainer.payMethodLack);
        rllp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        // rllp.addRule(RelativeLayout.ALIGN_BASELINE,
        // PayMethodContainer.payMethodVirtual);
        // rllp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
        rllp.addRule(RelativeLayout.ALIGN_BOTTOM,
                EPayMethodContainer.payMethodVirtual);
        rllp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
        rllp.addRule(RelativeLayout.RIGHT_OF,
                EPayMethodContainer.payMethodVirtualIv);

        // rllp.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
        // rllp.leftMargin = (int) TypedValue.applyDimension(
        // TypedValue.COMPLEX_UNIT_DIP, 38, dm);
        rllp.topMargin = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 5, dm);
        payMethodLack.setText("余额不足,请充值!!!");
        payMethodLack.setTextColor(0xffb89766);
        payMethodLack.setVisibility(View.GONE);
        payMethodLack.setEllipsize(TruncateAt.MARQUEE);
        payMethodLack.setLayoutParams(rllp);
        rl.addView(payMethodLack);

//        Drawable bmp = PayUtil.getDrawableFromJar(
//                "/images_mobilepay/loading.png", EPayMethodContainer.class);
        Drawable bmp =ResourcesCompat.getDrawable(getResources(), R.mipmap.loading, null);

        ImageView loading = new ImageView(getContext());
        loading.setId(EPayMethodContainer.loading);
        loading.setBackgroundDrawable(bmp);
        Animation rotaAnimation = new RotateAnimation(0, 357,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        loading.setAnimation(rotaAnimation);
        rotaAnimation.setRepeatCount(-1);
        rotaAnimation.setDuration(2000);
        rotaAnimation.setFillAfter(false);
        rotaAnimation.setInterpolator(new LinearInterpolator());
        rotaAnimation.startNow();
        int wh = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                20, dm);
        rllp = new RelativeLayout.LayoutParams(wh, wh);
        rllp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
        rllp.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
        rl.addView(loading);
        loading.setLayoutParams(rllp);

        FrameLayout balanceContainer = new FrameLayout(getContext());
        balanceContainer.setId(EPayMethodContainer.balanceContainer);
        balanceContainer.setVisibility(View.GONE);
        rl.addView(balanceContainer);
        balanceContainer.setLayoutParams(new RelativeLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        line = new View(getContext());
        line.setBackgroundDrawable(
//                PayUtil.getDrawableFromJar(
//                "/images_mobilepay/line.png", EPayMethodContainer.class)
                ResourcesCompat.getDrawable(getResources(), R.mipmap.line, null)
        );
        lp = new LayoutParams(LayoutParams.FILL_PARENT, 1);
        lp.gravity = Gravity.BOTTOM;

        rllp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, 1);
        rllp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
        line.setLayoutParams(rllp);
        rl.addView(line);

        line = new View(getContext());
        line.setBackgroundDrawable(
//                PayUtil.getDrawableFromJar(
//                "/images_mobilepay/line.png", EPayMethodContainer.class)
                ResourcesCompat.getDrawable(getResources(), R.mipmap.cw_icon, null)
        );
        lp = new LayoutParams(LayoutParams.FILL_PARENT, 1);
        addView(line);
        line.setLayoutParams(lp);

        ListView payMethodLV = new ListView(getContext());
        payMethodLV.setId(EPayMethodContainer.payMethodLV);
        payMethodLV.setLayoutParams(new ViewGroup.LayoutParams(
                LayoutParams.FILL_PARENT, (int) TypedValue.applyDimension(
                        TypedValue.COMPLEX_UNIT_DIP, itemCount * 80, dm)));
        payMethodLV.setBackgroundColor(Color.WHITE);
        payMethodLV.setDivider(
//                PayUtil.getDrawableFromJar(
//                "/images_mobilepay/line.png", EPayMethodContainer.class)
                ResourcesCompat.getDrawable(getResources(), R.mipmap.line, null)
        );
        payMethodLV.setDividerHeight(1);
        addView(payMethodLV);

        line = new View(getContext());
        line.setBackgroundDrawable(
//                PayUtil.getDrawableFromJar(
//                "/images_mobilepay/line.png", EPayMethodContainer.class)
                ResourcesCompat.getDrawable(getResources(), R.mipmap.line, null)
        );
        lp = new LayoutParams(LayoutParams.FILL_PARENT, 1);
        addView(line);
        line.setLayoutParams(lp);

        LinearLayout payPasswordContainer = new LinearLayout(getContext());
        payPasswordContainer.setId(EPayMethodContainer.payPasswordContainer);
        lp = new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);
        payPasswordContainer.setOrientation(LinearLayout.VERTICAL);
        payPasswordContainer.setGravity(LinearLayout.VISIBLE);
        payPasswordContainer.setVisibility(View.GONE);
        addView(payPasswordContainer);
        payPasswordContainer.setLayoutParams(lp);

        TextView payMethodPassword = new TextView(getContext());
        payMethodPassword.setId(EPayMethodContainer.payMethodPassword);
        lp = new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);
        padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                15, dm);
        payMethodPassword.setPadding(padding, padding, padding, padding);
        payMethodPassword.setText("习币支付密码");
        payMethodPassword.setTextColor(0xff747474);
        payMethodPassword.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        payPasswordContainer.addView(payMethodPassword);
        payMethodPassword.setLayoutParams(lp);

        line = new View(getContext());
        line.setBackgroundDrawable(
//                PayUtil.getDrawableFromJar(
//                "/images_mobilepay/line.png", EPayMethodContainer.class)
                ResourcesCompat.getDrawable(getResources(), R.mipmap.line, null)
        );
        lp = new LayoutParams(LayoutParams.FILL_PARENT, 1);
        payPasswordContainer.addView(line);
        line.setLayoutParams(lp);

        EditText payMethodPasswordET = new EditText(getContext());
        payMethodPasswordET.setId(EPayMethodContainer.payMethodPasswordET);
        payMethodPasswordET.setBackgroundColor(Color.WHITE);
        payMethodPasswordET.setHint("请输入习币支付密码");
        payMethodPasswordET.setInputType(InputType.TYPE_CLASS_TEXT
                | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD);
        int paddingLeft = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 10, dm);
        payMethodPasswordET.setPadding(paddingLeft, 0, 0, 0);
        lp = new LayoutParams(LayoutParams.FILL_PARENT,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                        60, dm));
        payPasswordContainer.addView(payMethodPasswordET);
        payMethodPasswordET.setLayoutParams(lp);

        line = new View(getContext());
        line.setBackgroundDrawable(
//                PayUtil.getDrawableFromJar(
//                "/images_mobilepay/line.png", EPayMethodContainer.class)
                ResourcesCompat.getDrawable(getResources(), R.mipmap.line, null)
        );
        lp = new LayoutParams(LayoutParams.FILL_PARENT, 1);
        payPasswordContainer.addView(line);
        line.setLayoutParams(lp);

    }
}
